package tcu.iot.gasmanage.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import tcu.iot.gasmanage.entity.equipment.Equipment;
import tcu.iot.gasmanage.entity.equipment.EquipmentQuery;
import tcu.iot.gasmanage.entity.Options;
import tcu.iot.gasmanage.mapper.provider.EquipmentProvider;

import java.util.List;

@Repository
@Mapper
@Component("EquipmentMapper")
public interface EquipmentMapper {

    //搜索所有设备的信息，返回Equipment格式的List集合
    @Select("SELECT * FROM equipment")
    List<Equipment> FindAllEquipment();

    //在EquipmentProvider中生成Sql语句，根据多条件实现设备信息的模糊查询
    @SelectProvider(type = EquipmentProvider.class, method = "SelectEquipment")
    List<Equipment> SelectEquipmentByMany(EquipmentQuery equipmentQuery);

    //根据模糊查询生成的Sql语句，查找符合条件设备的数量，返回total
    @SelectProvider(type = EquipmentProvider.class, method = "SelectEquipmentCount")
    int SelectEquipmentCount(EquipmentQuery equipmentQuery);

    //搜索所有设备的数量
    @Select("SELECT count(*) FROM equipment")
    int SelectEquipmentAllCount();

    @Select("SELECT equipmentNumber FROM equipment WHERE signalStrength=-1")
    List<String> GetEquipmentWithoutSignal();

    @Select("SELECT equipmentNumber FROM equipment WHERE signalStrength<>-1")
    List<String> GetEquipmentWithSignal();

    //获取equipmentType所有选项，返回List集合
    @Select("SELECT equipmentType FROM equipment WHERE equipmentType IS NOT NULL GROUP BY equipmentType")
    @Results({@Result(column = "equipmentType", property = "key"), @Result(column = "equipmentType", property = "label")})
    List<Options> FindEquipmentTypeOptions();

    //获取所有stateOptions所有选项
    @Select("SELECT * FROM stateOptions")
    List<Options> FindStateOptions();

    //获取所有省份信息选项
    @Select("SELECT province FROM equipment WHERE province IS NOT NULL GROUP BY province")
    @Results({@Result(column = "province", property = "key"), @Result(column = "province", property = "label")})
    List<Options> FindProvinceOptions();

    //获取所有数据库上传频率选项
    @Select("SELECT uploadFrequency FROM equipment WHERE uploadFrequency IS NOT NULL GROUP BY uploadFrequency")
    @Results({@Result(column = "uploadFrequency", property = "key"), @Result(column = "uploadFrequency", property = "label")})
    List<Options> FindUploadFrequencyOptions();

    //获得数据库所有设备名称和number，一一对应
    @Select("SELECT equipmentNumber,equipmentName FROM equipment")
    @Results({@Result(column = "equipmentNumber", property = "key"), @Result(column = "equipmentName", property = "label")})
    List<Options> FindAllEquipmentNumberName();

    //获得数据库所有管理员数据
    @Select("SELECT principal FROM equipment WHERE principal IS NOT NULL GROUP BY principal")
    @Results({@Result(column = "principal", property = "key"), @Result(column = "principal", property = "label")})
    List<Options> FindAllPrincipal();

    //实现数据库更新，通过传入物联网数据库实时更新的电池电量以及信号强度信息
    //对mysql数据库中的相关字段进行更新
    @Update("UPDATE equipment SET electricQuantity=#{electricQuantity},signalStrength=#{signalStrength} WHERE equipmentNumber=#{equipmentNumber}")
    void UpdatePower(double electricQuantity, float signalStrength, String equipmentNumber);

    //通过传入的id，对设备进行删除操作
    @Delete("DELETE FROM equipment WHERE id=#{id}")
    boolean DeleteEquipment(int id);

    //通过前端页面传入的实体类，实现新增设备功能
    @Insert("INSERT INTO equipment (id,equipmentNumber,equipmentName,equipmentType,signalStrength,state,uploadFrequency,province,region,site,pipeline,management,principal," +
            "additionTime,patrolInspectionTime,onlineTime,electricQuantity,remark,lat,lng) VALUES (#{id},#{equipmentNumber},#{equipmentName},#{equipmentType},#{signalStrength},#{state},#{uploadFrequency}," +
            "#{province},#{region},#{site},#{pipeline},#{management},#{principal},#{additionTime},#{patrolInspectionTime},#{onlineTime},#{electricQuantity},#{remark},#{lat},#{lng})")
    void InsertEquipment(int id, String equipmentNumber, String equipmentName, String equipmentType, float signalStrength, String state, String uploadFrequency, String province, String region,
                         String site, String pipeline, String management, String principal, String additionTime, String patrolInspectionTime, String onlineTime, float electricQuantity, String remark, String lat, String lng);

    //GetTrueEquipmentNumber
    @Select("SELECT equipmentNumber FROM equipment")
    List<String> CheckWithTdengine();

    //判断传入的equipmentNumber在表中是否存在
    @Select("SELECT * FROM equipment WHERE equipmentNumber=#{equipmentNumber}")
    boolean IsExist(String equipmentNumber);

    @Update("UPDATE equipment SET equipmentNumber=#{equipmentNumber},equipmentName=#{equipmentName},equipmentType=#{equipmentType},signalStrength=#{signalStrength},state=#{state},uploadFrequency=#{uploadFrequency},province=#{province},region=#{region},site=#{site},pipeline=#{pipeline},management=#{management},principal=#{principal}," +
            "additionTime=#{additionTime},patrolInspectionTime=#{patrolInspectionTime},onlineTime=#{onlineTime},electricQuantity=#{electricQuantity},remark=#{remark},lat=#{lat},lng=#{lng} WHERE id=#{id}")
    boolean UpdateEquipment(int id, String equipmentNumber, String equipmentName, String equipmentType, float signalStrength, String state, String uploadFrequency, String province, String region,
                            String site, String pipeline, String management, String principal, String additionTime, String patrolInspectionTime, String onlineTime, float electricQuantity, String remark, String lat, String lng);

    @Select("SELECT equipmentName FROM equipment WHERE equipmentNumber=#{equipmentNumber}")
    String FindEquipmentName(String equipmentNumber);
}
